<template>
  <div class="dashboard">
    <div class="system">招聘网站管理系统</div>

    <!-- 用户信息区域 -->
    <div class="user" @click.stop>
      <el-avatar class="avatar" :src="userInfo?.avatar || 'https://picsum.photos/200/200?random=1'" />
      <span class="username">{{ userInfo?.name || '加载中...' }}</span>
      <!-- 下拉菜单组件 -->
      <el-dropdown trigger="click" class="user-dropdown" ref="userDropdown">
        <i class="el-icon-caret-bottom" style="font-size: 15px; margin-left: 5px;"></i>
        <el-dropdown-menu slot="dropdown" class="user-menu">
          <el-dropdown-item  @click.native="goToProfile">个人中心
          </el-dropdown-item>
          <el-dropdown-item  @click.native="logout">退出登录
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </div>
</template>

<script>
import { mapGetters, mapActions } from 'vuex';


export default {
  computed: {
    ...mapGetters('manager', ['userInfo', 'token','ws'])
  },
  created() {
    // 仅当token存在且userInfo为空时才获取信息
    if (this.token && (!this.userInfo || !this.userInfo.userId)) {
      this.fetchManagerInfo().catch(error => {
        console.error('获取管理员信息失败:', error);
        this.$message.error('获取用户信息失败');
      });
    }
  },
  methods: {
    ...mapActions('manager', ['fetchManagerInfo', 'logout']),

    // 跳转到个人中心
    goToProfile() {
      this.$router.push({ name: 'manager-center' });
    },


    // 退出登录
    logout() {
      this.$confirm('确定要退出登录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$store.commit('manager/CLEAR_STATE');
        if(this.ws) this.ws.close();
        this.$message({
          type: 'success',
          message: '已退出登录'
        });
        this.$router.push('/login');
      }).catch(() => {
        // 取消退出
      });
    }
  }
}
</script>

<style scoped>
.dashboard {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 60px;
  background-color: #fff;
  white-space: nowrap;
}

.system {
  font-size: 20px;
  font-weight: bold;
  color: #333;
  min-width: 200px;
  height: 100%;
  display: flex;
  align-items: center;
}

.user {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 0 10px;
  height: 100%;
}

.avatar {
  width: 36px !important;
  height: 36px !important;
  margin-right: 8px;
  border-radius: 50% !important;
  overflow: hidden;
}

.username {
  color: #333;
  margin-right: 5px;
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-dropdown {
  position: relative;
  display: inline-flex;
  align-items: center;
}

.user-dropdown .el-icon-caret-bottom {
  transition: transform 0.2s ease;
}

.user-dropdown.opened .el-icon-caret-bottom {
  transform: rotate(180deg);
}

.user-menu {
  border-radius: 4px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  margin-top: 5px !important;
  min-width: 120px;
}

.user-menu .el-dropdown-item {
  padding: 8px 16px;
  font-size: 14px;
  transition: background-color 0.2s;
}

.user-menu .el-dropdown-item:hover {
  background-color: #f5f7fa;
}


@media (max-width: 768px) {
  .system {
    font-size: 16px;
  }
  
  .username {
    max-width: 80px;
  }
}
</style>